//
//  ViewController.m
//  test
//
//  Created by MacDev02 on 2019/1/21.
//  Copyright © 2019年 www.schinta.com.cn. All rights reserved.
//



#import "BViewController.h"

#import "EZRJSVM.h"
#import <EasyReact/EasyReact.h>
#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (strong, nonatomic) EZRJSVM *jsvm;
@property (nonatomic,strong)EZRMutableNode *node;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.textField  = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 100, 50)];
    
    self.node = [EZRMutableNode new];
    [self.textField.ezr_textNode linkTo:self.node];
    
    // calculate again if map text field changes
//    @ezr_weakify(self)
//    [[self.textField.ezr_textNode listenedBy:self] withBlock:^(NSString * _Nullable next) {
//        @ezr_strongify(self)
//        self.textField.text = self.mapBlock(self.textField.text);
//    }];
    
    self.node.value = @"滴滴第！";
    [self.view addSubview:self.textField];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 200, 100, 40)];
    [btn setTitle:@"跳转" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
    
    [btn.ezr_textNode linkTo:self.node];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 100, 40)];
    [label.ezr_textNode linkTo:self.node];
    
    [self.view addSubview:label];
}

- (void)pushVC{
    BViewController *bVc = [[BViewController alloc] initWithNode:self.node];
    [self.navigationController pushViewController:bVc animated:YES];

}
- (EZSMapBlock)mapBlock {
    @ezr_weakify(self)
    return ^id _Nullable(NSString * _Nullable next) {
        @ezr_strongify(self)
        JSValue *value = [self.jsvm eval:self.textField.text params:@{@"a": self.textField.text}];
        return [value toString];
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
